package com.whros.android.sample;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.whros.android.sample.helper.BitmapHelper;
import com.whros.android.sample.util.BitmapUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class BitmapActivity extends AppCompatActivity {

    List<ImageView> imageViewList = new ArrayList<>();
    LinearLayout containerLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bitmap);

        containerLayout = (LinearLayout) findViewById(R.id.containerLayout);
        setBitmapImageView();
        new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
            @Override
            public void run() {
                getBitmapSize(imageViewList);
            }
        },500);

    }

    private void setBitmapImageView(){
        containerLayout.removeAllViews();
        imageViewList.clear();

        TextView textView = new TextView(this);
        StringBuilder sb = new StringBuilder();
        DisplayMetrics metrics = this.getResources().getDisplayMetrics();
        sb.append("screen width=").append(metrics.widthPixels)
        .append(" height="+metrics.heightPixels)
        .append(" density="+metrics.density)
        .append(" densityDpi="+metrics.densityDpi);
        textView.setText(sb.toString());
        containerLayout.addView(textView);

        containerLayout.addView(createViewItemByImageFile("/sdcard/mtl_appicon_xh.png"));
        containerLayout.addView(createViewItem(R.drawable.mtl_appicon_h,"hdpi"));
        containerLayout.addView(createViewItem(R.drawable.mtl_appicon_xh,"xhdpi"));
        containerLayout.addView(createViewItem(R.drawable.mtl_appicon_xxh,"xxhdpi"));
        containerLayout.addView(createViewItem(R.drawable.mtl_appicon_xxxh,"xxxhdpi"));

    }

    private View createViewItem(int rid,String dir){
        LayoutInflater inflater = LayoutInflater.from(this);
        View rootView = inflater.inflate(R.layout.bitmap_item,null);
        ((TextView)rootView.findViewById(R.id.imgName)).setText(dir);
        ImageView imgView = (ImageView)rootView.findViewById(R.id.image);
        imgView.setImageResource(rid);

        imageViewList.add(imgView);
        return rootView;
    }

    private View createViewItemByImageFile(String path){
        LayoutInflater inflater = LayoutInflater.from(this);
        View rootView = inflater.inflate(R.layout.bitmap_item,null);
        ((TextView)rootView.findViewById(R.id.imgName)).setText(path);
        ImageView imgView = (ImageView)rootView.findViewById(R.id.image);
        imgView.setImageDrawable(Drawable.createFromPath(new File(path).getAbsolutePath()));
        imageViewList.add(imgView);
        return rootView;
    }

    private void getBitmapSize(List<ImageView> imageViews){

        for(ImageView imageView:imageViews){
            TextView textView = (TextView) ((ViewGroup)imageView.getParent()).findViewById(R.id.text);
            BitmapDrawable bd = (BitmapDrawable)imageView.getDrawable();
            if(bd == null){
                continue;
            }
            Bitmap bitmap = bd.getBitmap();

            StringBuilder sb = new StringBuilder();
            sb.append("内存中：width=").append(bitmap.getWidth()).append("  height=").append(bitmap.getHeight())
                    .append(" bitmapSize=").append(BitmapUtils.formatMemorySize(bitmap.getHeight()*bitmap.getRowBytes()))
                    .append("控件：imageView width=").append(imageView.getWidth())
                    .append(" height=").append(imageView.getHeight());
            textView.setText(sb.toString());
        }
    }
}
